var React = require('react');


var Bheader = require('../widget/Bheader');
var Footer = require('../widget/Footer');
var ajax = require('../tools/ajax');
var Photo = require('../widget/Photo');
var Icon = require('../components/Icon');

module.exports = React.createClass({
    getInitialState: function () {
        return {
            theme:"Default",
            userInfo:"",
            headPortrait:"",//头像
            flag:false,//拍照界面判断
        }
    },

    componentDidMount: function () {
        this.getUserInfoByUid();
    },

    //获取用户信息
    getUserInfoByUid(){
        var p = this;
        var userId = localStorage.getItem("userId");
        if(userId) {
            ajax("POST", "getUserInfoByUid", {userId: userId}, function (dt) {
                if (dt && dt.success) {
                    var objs = dt.data[0];
                    console.log(objs);
                    p.setState({userInfo:objs});
                }
            })
        };
    },
    action(e){
        var action = e.currentTarget.getAttribute("action");
        location.href = action;
    },

    quit:function (e) {
        localStorage.clear();
        localStorage.setItem("shopId",this.state.userInfo.shopid);
        this.action(e);
    },
    //拍照界面判断
    photo(){
        this.refs.Photo.setState({flag:this.state.flag});
        var flag = this.state.flag;
        if(flag == false){
            this.setState({flag:true})
        }
    },
    //照片添加
    picUrl(){
        var p = this;
        var purl = p.refs.Photo.state.photoUrl;
        var userId = localStorage.getItem("userId");
        p.setState({headPortrait:purl});
        ajax("POST","headPortrait",{userId:userId,photo:purl},function(dt){
            if(dt&&dt.success){
                console.log("头像OK")
            }
        })
    },
    render: function () {
        var p =this;
        var userInfo = p.state.userInfo;
        console.log(userInfo);
        return (
            <div className="main"  id="main">
                <Bheader theme={p.state.theme} title="个人资料" headr="false" hact="#wode"></Bheader>
                <div className="container-t">
                    <div className="user-box">
                        <div className="order-info">
                            <div className="item-text" onClick={p.photo}>
                                <p className="order-title">头像</p>
                                <img className="user-img" src={p.state.headPortrait?p.state.headPortrait:p.state.userInfo.photo} />
                                <Icon icon="#icon-fanhui"></Icon>
                            </div>
                            <div className="item-text">
                                <p className="order-title">账户名</p>
                                <p className="order-all">{p.state.userInfo.account}</p>
                            </div>
                            <div className="item-text" onClick={p.action} action="#nikename">
                                <p className="order-title">昵称</p>
                                <p className="order-all">
                                    {p.state.userInfo.nickname == null?"未设置":p.state.userInfo.nickname}
                                </p>
                                <Icon icon="#icon-fanhui"></Icon>
                            </div>
                            <div className="item-text">
                                <p className="order-title">会员等级</p>
                                <p className="order-all">白银会员</p>
                            </div>
                            <div className="item-text">
                                <p className="order-title">手机绑定</p>
                                <p className="order-all">{p.state.userInfo.phone}</p>
                            </div>
                        </div>
                    </div>
                    <div className="user-btn">
                        <div className="btn_login" onClick={p.quit} action="#wode">
                            <span>退出登录</span>
                        </div>
                    </div>
                </div>
                <Photo ref="Photo" picUrl={p.picUrl}/>
            </div>
        )
    }
});
